
package com.sfdlJPush.controller;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import cn.jpush.android.api.JPushInterface;

import com.sfdlJPush.R;
import com.sfdlJPush.utils.EventBus;
import com.sfdlJPush.utils.EventBus.SubscriberChangeListener;
import com.sfdlJPush.utils.EventNames;

public class MessageNewFragment extends B5MBaseFragment implements SubscriberChangeListener
{
    private ListView mListView;
    private MessageNewAdapter messageNewAdapter;

    @Override
    public int doGetContentViewId()
    {
        // TODO Auto-generated method stub
        return R.layout.message_new;
    }

    @Override
    public void doInitSubViews(View containerView)
    {
        // TODO Auto-generated method stub
        mListView = (ListView) containerView.findViewById(R.id.listview);
    }

    @Override
    public void doInitDataes()
    {
        // TODO Auto-generated method stub
        messageNewAdapter = new MessageNewAdapter(mActivity, new ArrayList<String>());
        mListView.setAdapter(messageNewAdapter);
    }
    //for receive customer msg from jpush server
    public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION";
    public static final String KEY_TITLE = "title";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EXTRAS = "extras";

    @Override
    public void doAfter()
    {
        // TODO Auto-generated method stub
    }

    @Override
    public void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        EventBus.getInstance().subscribe(EventNames.MESSAGE_NEW, this);
        EventBus.getInstance().subscribe(EventNames.NOTIFICATION_NEW, this);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getInstance().unSubscribe(EventNames.MESSAGE_NEW, this);
        EventBus.getInstance().unSubscribe(EventNames.NOTIFICATION_NEW, this);
    }

    @Override
    public void onSubscriberDataChanged(Object notificationName , Object notificateContent)
    {
        // TODO Auto-generated method stub
        if (EventNames.MESSAGE_NEW.equals(notificationName))
        {
            Bundle bundle = (Bundle) notificateContent;
            String messge = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            updateAdapter(messge);
        }
        else if (EventNames.NOTIFICATION_NEW.equals(notificationName))
        {
            Bundle bundle = (Bundle) notificateContent;
            String messge = bundle.getString(JPushInterface.EXTRA_ALERT);
            updateAdapter(messge);
        }
    }

    public void updateAdapter(String messge)
    {
        messageNewAdapter.list.add(messge);
        messageNewAdapter.notifyDataSetChanged();
    }
}
